package com.hangwei.paperless;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.hangwei.paperless.adapter.ProcessListAdapter;
import com.hangwei.paperless.model.bean.ProcessSheet;
import com.hangwei.paperless.presenter.ProcessListPresenter;
import com.hangwei.paperless.view.ProcessListTabTitle;
import com.hangwei.paperless.zxing.QRCodeScanActivity;
import com.twelve.all.base.BaseActivity;
import com.twelve.all.bean.ClearBottom;
import com.twelve.all.net.CallBack;
import com.twelve.all.utils.LoadingDialog;
import com.twelve.all.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class ProcessListActivity extends BaseActivity {

    private ProcessListAdapter mAdapter;
    private ProcessListPresenter mPresenter;
    private LoadingDialog mLoadingDialog;

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onClearBottom(ClearBottom event) {
        hideSystemBar();
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_process_list);
        EventBus.getDefault().register(this);
        findViewById(R.id.back).setOnClickListener(v -> finish());
        EditText etNum = findViewById(R.id.etNum);
        findViewById(R.id.tvSearch).setOnClickListener(v -> {
            String num = etNum.getText().toString().trim();
            mPresenter.setWoNo(num);
            loadingRefresh();
        });
        findViewById(R.id.ivScan).setOnClickListener(v -> {
            if (Build.VERSION.SDK_INT >= 23 &&
                    ContextCompat.checkSelfPermission(ProcessListActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
                ActivityCompat.requestPermissions(ProcessListActivity.this,
                        new String[]{
                                Manifest.permission.CAMERA
                        }, 100);
            else startActivity(new Intent(ProcessListActivity.this, QRCodeScanActivity.class));
        });

        LinearLayout llTitle = findViewById(R.id.llTitle);
        ProcessListTabTitle tabTitle = new ProcessListTabTitle(this, llTitle);
        tabTitle.setL((pageOrderName, pageOrderBy) -> {
            mPresenter.setPageOrderBy(pageOrderBy);
            mPresenter.setPageOrderName(pageOrderName);
            loadingRefresh();
        });
        llTitle.addView(tabTitle.getInflate());
        SwipeRefreshLayout refreshLayout = findViewById(R.id.refreshLayout);
        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        mAdapter = new ProcessListAdapter(null);
        mPresenter = new ProcessListPresenter(refreshLayout, mAdapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(mAdapter);
        recyclerView.addOnItemTouchListener(new OnItemChildClickListener() {
            @Override
            public void onSimpleItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                ProcessSheet processSheet = mAdapter.getData().get(position);
                Intent intent;
                switch (view.getId()) {
                    case R.id.tvProgressSheet:
                        intent = new Intent(ProcessListActivity.this, ProcessActivity.class);
                        intent.putExtra("info", processSheet);
                        startActivity(intent);
                        break;
                    case R.id.tvCardList:
                        intent = new Intent(ProcessListActivity.this, CardListActivity.class);
                        intent.putExtra("info", processSheet);
                        startActivity(intent);
                        break;
                    case R.id.tvCardProcess:
                        intent = new Intent(ProcessListActivity.this, CardProcessActivity.class);
                        intent.putExtra("info", processSheet);
                        startActivity(intent);
                        break;
                    case R.id.tvCardFirstCheck:
                        intent = new Intent(ProcessListActivity.this, CardFirstCheckActivity.class);
                        intent.putExtra("info", processSheet);
                        startActivity(intent);
                        break;
                }
            }
        });
        loadingRefresh();
    }

    private void loadingRefresh() {
        if (mLoadingDialog == null)
            mLoadingDialog = new LoadingDialog(ProcessListActivity.this);
        if (!mLoadingDialog.isShowing()) mLoadingDialog.show();
        mPresenter.refresh(new CallBack<List<ProcessSheet>>() {
            @Override
            public void onCompleted(List<ProcessSheet> processSheets) {
                if (mLoadingDialog != null && mLoadingDialog.isShowing())
                    mLoadingDialog.dismiss();
            }

            @Override
            public void onError() {
                if (mLoadingDialog != null && mLoadingDialog.isShowing())
                    mLoadingDialog.dismiss();
            }
        });
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Intent intent = new Intent(ProcessListActivity.this, QRCodeScanActivity.class);
                intent.putExtra("type", 0);
                startActivity(intent);
            } else {
                ToastUtil.showCenterToast("无权限！");
            }
        }
    }
}
